from pymongo import MongoClient, DESCENDING
import pymongo
import csv


MONGO_URI = "mongodb://admin:123456@10.62.170.203:27017/crawl?authSource=admin"

# 连接MongoDB
def connect_mongodb():
    """连接MongoDB数据库"""
    # 连接字符串
    mongo_client = MongoClient(MONGO_URI)

    return mongo_client['crawl']

# 导出folder_id到CSV
def export_folder_ids_to_csv(db, output_file):
    """从MongoDB中导出所有folder_id到CSV文件"""
    # 获取集合
    collection = db['project_test']
    
    # 查询所有document，并提取folder_id
    folder_ids = []
    cursor = collection.find({}, {'folderId': 1})  # 只获取'id'字段
    for item in cursor:
        if 'folderId' in item:
            folder_ids.append(item['folderId'])
    
    print(len(set(folder_ids)))



# 导出到CSV文件
output_file = 'method_ids.csv'

# 连接数据库并导出
db = connect_mongodb()
export_folder_ids_to_csv(db,  output_file)
